package cn.kuang.service;

import cn.kuang.mapper.AdminMapper;
import cn.kuang.mapper.AdminRoleMapper;
import cn.kuang.mapper.RoleMapper;
import cn.kuang.pojo.Administrator;
import cn.kuang.pojo.Role;
import cn.kuang.pojo.adminRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author 徽州大都督
 * @date 2020/7/19
 */
@Transactional
@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private AdminRoleMapper adminRoleMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询所有
     *
     * @return
     */
    public List<Administrator> findAll() {
        List<Administrator> administrators = null;
        if (redisTemplate.opsForValue ().get ("adminList") != null) {
            administrators = (List<Administrator>) redisTemplate.opsForValue ().get ("adminList");

            //System.out.println ("缓存中查询");
        }else {
            administrators = adminMapper.selectList (null);
            administrators.forEach (administrator -> {
                adminRole adminRole = adminRoleMapper.selectById (administrator.getAdminId ());

                Role role = roleMapper.selectById (adminRole.getRid ());
                administrator.setRole (role);
            });

            redisTemplate.opsForValue ().set ("adminList",administrators,10, TimeUnit.MINUTES);
            //System.out.println ("数据库中查询");
        }



        return administrators;
    }

    /**
     * 添加
     *
     * @param administrator
     * @return
     */
    public int addAdmin(Administrator administrator) {

        //随机生成一个uid，专门设置管理员和角色
        String aid = UUID.randomUUID ().toString ();
        administrator.setAdminId (aid);
        administrator.setLoginTime (new Date ());
        //administrator.setDeleted (0);
        int insert = adminMapper.insert (administrator);

        QueryWrapper wrapper = new QueryWrapper ();
        wrapper.eq ("role_name", administrator.getRoleName ());
        Role role = roleMapper.selectOne (wrapper);
        String roleId = role.getRoleId ();
        adminRole adminRole = new adminRole ();
        adminRole.setAid (aid);
        adminRole.setRid (roleId);
        adminRoleMapper.insert (adminRole);


        return insert;
        //System.out.println ("----------------------------------");
        //System.out.println (role);
    }

    /**
     * 登录验证
     *
     * @param username
     * @param password
     * @return
     */
    public Administrator login(String username, String password) {
        QueryWrapper<Administrator> wrapper = new QueryWrapper<> ();
        wrapper.eq ("admin_name", username);
        wrapper.eq ("password", password);
        Administrator administrator = adminMapper.selectOne (wrapper);
        if (administrator != null) {
            adminRole adminRole = adminRoleMapper.selectById (administrator.getAdminId ());
            Role role = roleMapper.selectById (adminRole.getRid ());
            administrator.setRole (role);

            Integer loginCount = administrator.getLoginCount ();

            administrator.setLoginCount (loginCount + 1);

            //保存上次登录时间
            Date loginTime = administrator.getLoginTime ();


            QueryWrapper wrapper1 = new QueryWrapper ();
            wrapper1.eq ("admin_id", administrator.getAdminId ());
            adminMapper.update (administrator, wrapper1);

            //将上次登录时间设置进去
            administrator.setLoginTime (loginTime);
            return administrator;
        } else {
            return null;
        }


    }

    /**
     * 根据id查询
     *
     * @param aid
     * @return
     */
    public Administrator findAdminById(String aid) {

        if (StringUtils.isEmpty (aid)) {
            return new Administrator ();
        }

        Administrator administrator = adminMapper.selectById (aid);

        if (administrator != null) {
            adminRole adminRole = adminRoleMapper.selectById (aid);
            String rid = adminRole.getRid ();
            Role role = roleMapper.selectById (rid);
            administrator.setRole (role);
        }


        return administrator;
    }

    /**
     * 修改
     *
     * @param adminId
     * @param administrator
     */
    public int updateById(String adminId, Administrator administrator) {

        //System.out.println (administrator);
        administrator.setAdminId (adminId);
        QueryWrapper<Role> wrapper = new QueryWrapper<> ();
        wrapper.eq ("role_name", administrator.getRoleName ());
        Role role = roleMapper.selectOne (wrapper);
        adminRole adminRole = new adminRole ();
        adminRole.setRid (role.getRoleId ());
        adminRole.setAid (administrator.getAdminId ());
        //adminRoleMapper.insert (adminRole);
        adminRoleMapper.updateById (adminRole);
        int i = adminMapper.updateById (administrator);
        return i;
    }

    /**
     * 删除
     *
     * @param id
     */
    public int delAdmin(String id) {

        Administrator administrator = adminMapper.selectById (id);

        if (redisTemplate.opsForValue ().get ("adminList")!=null){
            List<Administrator> adminList= (List<Administrator>) redisTemplate.opsForValue ().get ("adminList");
            if (adminList.contains (administrator)){
                adminList.remove (administrator);
                redisTemplate.opsForValue ().set ("adminList",adminList);
            }
        }

        int row = adminMapper.deleteById (id);
        return row;
    }

    /**
     * 模糊搜索
     * @param newKey
     * @return
     */

    public List<Administrator> findByKey(String newKey) {
        QueryWrapper wrapper = new QueryWrapper ();

        wrapper.like ("admin_name",newKey);

        List<Administrator> adminList = adminMapper.selectList (wrapper);

        return adminList;
    }
}
